﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tom.Models;

namespace Tom.Controllers
{
    public class HomeController : Controller
    {
        private IHostingEnvironment _environment;

        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            _environment = hostingEnvironment;
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public ActionResult Upload(IFormFile fileinput)
        {
            string dir = Path.Combine(_environment.ContentRootPath, "UploadFiles");
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            var fullpath = Path.Combine(dir, DateTime.Now.Ticks.ToString(), Path.GetExtension(fileinput.FileName));
            using (FileStream fileStream = new FileStream(fullpath, FileMode.OpenOrCreate))
            {
                fileinput.CopyTo(fileStream);
            }
            return Json(null);
        }
    }
}
